<?php
namespace common\queue\jobs;

use think\queue\Job;
use common\queue\JobAbstract;

use common\service\ChainService;
use common\service\QueueService;
use common\service\BotInvService;

use think\Exception;
use think\Log;

class AssetOutJob extends JobAbstract
{
	public function assetOutJob(Job $job, $data)
	{
		try {
			$userId = $data['user_id'];
			$botInvIds = $data['bot_inv_ids'];

			$count = BotInvService::getInstance()->checkWaitTakingInvs($userId, $botInvIds);
			if (!$count) {
				throw new Exception('query bot inv failed, botinvids:'.json_encode($botInvIds));
			}

			$invs = implode(',', $botInvIds);
			$res = ChainService::getInstance()->takeBack($userId, $invs);
			if (!$res) {
				QueueService::getInstance()->setAdapter('AssetOutJob')->assetOutJob($userId, $botInvIds);
				// take back bot_inv_id
				throw new Exception('take back failed, botinvids:'.$invs);
			}

			$job->delete();
			echo 'take back success, botinvids:'.$invs."\r\n";
		} catch (Exception $e) {
			echo $e->getMessage()."\r\n";
			$job->delete();
			return false;
		}
	}
}
